#include "musiclist.h"

#include <QMimeDatabase>
#include <QMimeType>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include "music.h"

MusicList::MusicList()
{

}

void MusicList::addMusicsByUrls(QList<QUrl> &list)
{
    //添加一次检查，检测歌曲文件的MIME类型

    for(auto& e : list)
    {
        QString filePath = e.toLocalFile();
        if(musicPaths.contains(filePath))
        {
            qDebug() << "***************";
            qDebug() << "filePath contained: "<< filePath;
            continue;
        }
        else
        {
            qDebug() << "***************";
            qDebug() << "filePath not contained: "<< filePath;
            musicPaths.insert(filePath);
        }

        //通过QMimeDatabase对象来获取对象数据，目的是检查文件是否符合要求
        QMimeDatabase mimedb;
        QMimeType mimetype = mimedb.mimeTypeForFile(e.toLocalFile());
        QString mime = mimetype.name();

        if(mime != "audio/mpeg" && mime != "audio/flac")
        {
            continue;
        }

        //这里采取的想法是通过music的第二个构造函数，通过给予一个url
        //让music通过QMediaPlayer和QMetaData来自动获取这个url所
        //指向的文件的信息，完成剩余构造
        musicList.push_back(e);
    }
}

Iterator MusicList::begin()
{
    return musicList.begin();
}

Iterator MusicList::end()
{
    return musicList.end();
}

Iterator MusicList::findMusicByMusicId(const QString &musicId)
{
    for(auto it = begin(); it != end(); ++it)
    {
        if(it->getMusicId() == musicId)
        {
            return it;
        }
    }
    return end();
}

void MusicList::writeToDB()
{
    //每次退出程序时，将收藏记录同步到我们的数据库中
    for(auto& e : musicList)
    {
        e.writeMusicInfo();
    }
}

void MusicList::readFromMusicListDB()
{
    //QString sql("select musicId, musicName, musicSinger, musicAlbum, duration, isLike, isHistroy, musicUrl, musicCover from musicInfo;");
    //QSqlQuery query("select musicId, musicName, musicSinger, musicAlbum, duration, isLike, isHistroy, musicUrl, musicCover from musicInfo;");
    QSqlQuery query("select musicId, musicName, musicSinger, musicAlbum, duration, isLike, isHistory, musicUrl from musicInfo;");
    if(!query.exec())
    {
        qDebug() << "read from musicInfo fail" << query.lastError().text();
        return;
    }
    qDebug() << "read from musicInfo success";

    while(query.next())
    {
        QString tmp = query.value(7).toString();
        musicPaths.insert(tmp);
        //tmp.insert(0, "file://");
        qDebug() << "***************";
        qDebug() << "musicPaths in read: "<<tmp;

        Music music(tmp);
        music.setMusicId(query.value(0).toString());
        music.setMusicName(query.value(1).toString());
        music.setMusicSinger(query.value(2).toString());
        music.setMusicAlbum(query.value(3).toString());
        music.setDuration(query.value(4).toLongLong());
        music.setIsLike(query.value(5).toBool());
        music.setIsHistory(query.value(6).toBool());

        musicList.push_back(music);
    }
}

Iterator MusicList::findMusicByMusicName(const QString& musicName)
{
    for(auto it = begin(); it != end(); it++)
    {
        if(it->getMusicName() == musicName)
        {
            return it;
        }
    }
    return end();
}

bool MusicList::isEmpty()
{
    return musicList.isEmpty();
}
